package com.hewep.demo.nio;

import java.util.ArrayList;
import java.util.List;

public class Notify {

	private static Notify instance = null;
	private static List<ServerListener> listeners = new ArrayList<ServerListener>();
	
	public static synchronized Notify getInstance(){
		if (instance == null) {
			instance = new Notify();
			return instance;
		}else {
			return instance;
		}
	}
	
	//将监听的处理事件注册到通知器的队列中
	public void addListener(ServerListener s){
		synchronized (listeners) {
			if (!listeners.contains(s)) {
				listeners.add(s);
			}
		}
	}
	
	
	//通知处理事件
	public void fireError(String error) {
		for (ServerListener sl : listeners) {
			sl.onError(error);
		}
	}
	
	
	public void fireOnAccept() throws Exception{
		for (ServerListener sl : listeners) {
			sl.onAccept();
		}
	}
	
	public void fireOnRead(Request request) throws Exception{
		for (ServerListener sl : listeners) {
			sl.onRead(request);
		}
	}
	
	public void fireOnWrite(Request request, Response response) throws Exception{
		for (ServerListener sl : listeners) {
			sl.onWrite(request, response);
		}
	}
	
}
